package com.intel.two.controller.medical;

import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;

import com.intel.two.pojo.medical.Health;
import com.intel.two.service.medical.HealthService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.medical.HealthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author:xiaofeia
 * @DATE:2022/10/15
 */
@RestController
@RequestMapping("/medical/health")
public class HealthController {
    @Autowired
    private HealthService healthService;


    @RequestMapping("/list")
    public LayuiPageVo<Health> healthList(@RequestParam Map<String, Object> params){
//        System.out.println(params);
        return healthService.healthPageData(params);
    }

    //添加设备信息
    @PostMapping("/add")
    public R addHealth(@RequestBody Health health){
//        System.out.println("armarium测试："+armarium);
        int result = healthService.addHealth(health);

        if(result==1){
            return R.ok();
        }else{
            return  R.error();
        }
    }
    //上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端

        return R.ok().data("url",fileUrl);

    }
    @GetMapping("/delete")
    public R deleteHealth(Integer health_id){
        //根据hid删除房屋信息
//        System.out.println("health_id" + health_id);
        int result = healthService.deleteHealthById(health_id);
        if(result ==1){
            return R.ok().message("删除成功");
        }
        return R.error();
    }
    @PostMapping("/deleteIds")
    public R deletIdsHealth(String health_ids){
//        System.out.println(armarium_ids);
        int result = healthService.deletHealthIds(health_ids);
        if(result > 0){
            return R.ok().message("删除选中成功");
        }
        return R.error();

    }
    @PostMapping("/edit")
    public R updateHealth(Health health){
        System.out.println(health);
        int result = healthService.updateHealth(health);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //柱状图展示信息
    @RequestMapping("/echarBar")
    public R echarBar(){
        List<HealthVo> list = healthService.sexBarData();
        System.out.println(list.toString());
        return R.ok().data("sexBar",list);
    }
}
